# coding: utf-8

import json


class Base(object):

    CLEAN_FIELDS = ('desc', 'csrfmiddlewaretoken')

    def __init__(self, task_path, task_name, task_id, **kwargs):
        self.task_path = task_path
        self.task_id = task_id
        self.task_name = task_name
        self.kwargs = kwargs
        self.hosts = []
        self.clean()

        self._run = None
        self._workspace = None
        self._inventory = None

    def clean(self):
        if isinstance(self.kwargs, basestring):
            try:
                self.kwargs = json.loads(self.kwargs)
            except:
                self.kwargs = {}
        if self.kwargs and isinstance(self.kwargs, dict):
            [self.kwargs.pop(field) for field in self.CLEAN_FIELDS if self.kwargs.has_key(field)]
        if not isinstance(self.kwargs, dict):
            self.kwargs = {}
        print self.kwargs
        if self.kwargs.has_key('task_hosts'):
            self.hosts = self.kwargs.pop('task_hosts')
        else:
            self.hosts = []

    def build(self):
        pass

    def run(self):
        self.build()
        status, output = self._execute()
        self.finished()
        return status, output

    def finished(self):
        pass

    def _execute(self):
        return '1', '2'
